package expection01;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class OneDay01 {
    public static void main(String[] args) throws Exception {
        //throwDemo();
        //tryCatchDemo();
        //tryCatchFinallyDemo();
        //autoCloseableDemo();
        //pwDemo();
        //brDemo();

    }

    private static void brDemo() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("asd.txt"),StandardCharsets.UTF_8));
        String len ;
        while ((len = br.readLine()) != null){
            System.out.println(len);
        }
        br.close();
    }

    private static void pwDemo() throws Exception {
        FileOutputStream fos = new FileOutputStream("asd.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw);
        while (true){
            Scanner scan = new Scanner(System.in);
            String line = scan.nextLine();
                if("exit".equals(line)){
                    break;
                }
                pw.println(line);
        }
        System.out.println("写入完毕");
        pw.close();
    }

    private static void autoCloseableDemo() {
        System.out.println("程序开始");
        try(FileOutputStream fos =new FileOutputStream("./a/asd.txt"); ){
            fos.write(11);
        }catch (IOException e){
            System.out.println("出现一个文件操作异常，已经被处理了");
        }
        System.out.println("程序结束");
    }


    private static void tryCatchFinallyDemo() {
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream("./a/asd.txt");
            fos.write(11);
        }catch (IOException e){
            System.out.println("出现一个文件操作异常，已经被处理了");
        }finally {
            try {
                fos.close();
            } catch (Exception e) {
                System.out.println("文件关闭时的异常已经处理");
            }
        }
    }

    private static void tryCatchDemo() {
        System.out.println("程序开始");
        try{
            String line = null;
            //System.out.println(line.length());
            String line1 = "";
            System.out.println(line1.charAt(0));
        }catch (NullPointerException e){
            System.out.println("发现空指针异常，并解决了");
        }catch (Exception e){
            System.out.println("其他异常已经被解决");
        }
        System.out.println("程序结束");
    }

    private static void throwDemo() {
        Student li = new Student();
        try {
            li.setAge(50);
        } catch (Exception e) {
            System.out.println("输入年龄不合法");
        }
        System.out.println("李四的年龄为:"+ li.getAge()+"岁");
        Student ww = new Student();
        try {
            ww.setAge(1000);
        } catch (Exception e) {
            System.out.println("输入年龄不合法");
        }
        System.out.println("王五的年龄为:"+ ww.getAge()+"岁");
    }
}
